<template>
	<view class="content">
		<navBar title="妙街代言" :change="true" ref="navHeader"></navBar>
		<view class="header-title flex align-center justify-center">
			<image src="/static/icon/endorsing.png" />
			<text>我要代言</text>
			<image src="/static/icon/endorsing.png" />
		</view>
		<view class="container">
			<view class="text-title">选择代言平台</view>
			<view class="forum-wrap flex justify-between align-center">
				<view class="forum-info flex align-center">
					<image src="/static/douyin.png" />
					<view class="name">{{picker[index]}}</view>
				</view>
				<view class="forum-select">
					<picker @change="PickerChange" :value="index" :range="picker">
						<view class="picker">
							选择平台
							<text class="cuIcon-right"></text>
						</view>
					</picker>
				</view>
			</view>
			<view class="cu-form-group">
				<view class="title">抖音号</view>
				<input placeholder="请输入抖音账号" name="input" v-model="number"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">代言作品链接</view>
				<input placeholder="请输入代言作品链接" name="input" v-model="url"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">
					上传代言作品截图
				</view>
			</view>
			<view class="padding-left grid col-3 grid-square flex-sub">
				<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
					<image :src="imgList[index]" mode="aspectFill"></image>
					<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
						<text class='cuIcon-close'></text>
					</view>
				</view>
				<view class="solids" @tap="ChooseImage" v-if="imgList.length<1">
					<text class='cuIcon-cameraadd'></text>
				</view>
			</view>
		</view>
		<view class="submit-wrap">
			<view class="submit" @click="addtoDouyin">提交代言</view>
		</view>
	</view>
</template>

<script>
	import Api from '@/common/api';
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import navBar from "@/components/header-nav";
	export default {
		components: {
			uniLoadMore,
			navBar
		},
		data() {
			return {
				scrollChange: false,
				picker: ['抖音'],
				index: 0,
				number: '',
				url: '',
				imgList: [],
				imgUrl: ''
			};
		},

		onLoad() {

		},
		onReady() {},
		methods: {
			// 发布作品
			async addtoDouyin() {
				let userInfos = uni.getStorageSync('userInfos') || '';
				let params = {
					'memberId': userInfos.id, //用户id
					'account': this.number, //抖音账号
					'pic': this.imgUrl, //图片地址
					'url': this.url, //作品链接
				};
				let data = await Api.apiCall('POST', 'member/share/add', params,false,"application/json");
				if(data == 1){
					uni.showToast({
						icon:'none',
						title:'提交成功！'
					})
				}
			},
			PickerChange(e) {
				this.index = e.detail.value
			},

			ChooseImage() {
				uni.showLoading({
					title: ""
				});
				var that = this
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					success: function(res) {
						//console.log(res.tempFilePaths)
						that.imgList = that.imgList.concat(res.tempFilePaths)
						uni.uploadFile({
							url: 'http://119.29.103.180:8085/aliyun/oss/upload1',
							filePath: that.imgList[0],
							name: 'file',
							success: function(obj) {
								var sdasd = JSON.parse(obj.data)
								that.imgUrl = sdasd.data
							}
						});
						if (that.imgList.length != '') {
							// that.canAdd = false;
						}
						uni.hideLoading();
					},
					fail: function(err) {
						//console.log('上传图片错误', err)
						uni.hideLoading();
					}
				});
			},

			// ChooseImage() {
			// 	uni.chooseImage({
			// 		count: 1, //默认9
			// 		sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			// 		sourceType: ['album'], //从相册选择
			// 		success: (res) => {
			// 			//console.log(res)
			// 			if (this.imgList.length != 0) {
			// 				this.imgList = this.imgList.concat(JSON.stringify(res.tempFilePaths))
			// 			} else {
			// 				this.imgList = res.tempFilePaths
			// 			}
			// 		}
			// 	});
			// },
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '删除',
					content: '确定要删除吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
		}
	};
</script>

<style lang="scss">
	page,
	.content {
		background: #f5f5f5;
		min-height: 100vh;
		position: relative;

		.header-title {
			width: 100%;
			padding: 40upx 0;

			image {
				width: 38upx;
				height: 52upx;
			}

			text {
				padding: 0 20upx;
				font-size: 36upx;
				color: #711F91;
				font-weight: bold;
			}
		}

		.container {
			background: #fff;
			border-radius: 20upx;
			padding: 30upx;

			.cu-form-group {
				margin: 10upx 0;
			}

			.forum-wrap {
				margin-top: 20upx;
				padding: 30upx;
				box-shadow: 2upx 2upx 10upx 0px rgba(250, 100, 0, 0.2);
				border-radius: 20upx;

				.forum-info {
					image {
						width: 64upx;
						height: 64upx;
					}

					.name {
						font-size: 32upx;
						color: #333;
						margin-left: 30upx;
					}
				}
			}
		}

		.submit-wrap {
			padding: 0 30upx;
			margin: 50upx 0;

			.submit {
				width: 100%;
				font-size: 36upx;
				color: #fff;
				font-weight: bold;
				text-align: center;
				padding: 26upx 0;
				border-radius: 44upx;
				background: linear-gradient(135deg, rgba(255, 119, 119, 1) 0%, rgba(255, 29, 29, 1) 100%);
			}
		}
	}
</style>
